package com.cqeec.bootdemo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cqeec.bootdemo.common.R;
import com.cqeec.bootdemo.dto.DishDto;
import com.cqeec.bootdemo.entity.Category;
import com.cqeec.bootdemo.entity.Dish;
import com.cqeec.bootdemo.entity.DishFlavor;
import com.cqeec.bootdemo.service.CategoryService;
import com.cqeec.bootdemo.service.DishFlavorService;
import com.cqeec.bootdemo.service.DishService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DishFlavorService dishFlavorService;

    //http://localhost:8080/dish/page?page=1&pageSize=10&name=1
    //多表分页模糊查询
//    @RequestMapping("/page")
//    public R<Page<DishDto>> getPage(int page, int pageSize, String name){
//        //确保参数取到
//        Page<Dish> pageInfo=new Page<>(page,pageSize);
//        LambdaQueryWrapper<Dish> wrapper=new LambdaQueryWrapper<>();
//        wrapper.like(StringUtils.isNotBlank(name),Dish::getName,name);
//        Page<Dish> page1=dishService.page(pageInfo,wrapper);
//        //多表
//        Page<DishDto> dishDtoPage=new Page<>();
//        //往空对象中赋值
//        //Stream流，lambda表达式
//        //对象拷贝，page1.records
//        BeanUtils.copyProperties(page1,dishDtoPage,"records");
//        //foreach 循环---->Stream
//        List<DishDto> records=page1.getRecords().stream().map(item -> {
//            //item 先当于一数据库表中的一条数据
//            DishDto dishDto=new DishDto();
//            //拷贝对象
//            BeanUtils.copyProperties(item, dishDto);
//            //根据数据中的categoryId 进行对category表的查询
//            Long categoryId=item.getCategoryId();
//            Category category=categoryService.getById(categoryId);
//            //对查询的数据进行赋值
//            if (category!=null){
//                dishDto.setCategoryName(category.getName());
//            }
//            return dishDto;
//        }).collect(Collectors.toList());
//        dishDtoPage.setRecords(records);
//        return R.success(dishDtoPage);
//    }
    @RequestMapping("/page")
    public R<Page<DishDto>> getPage(int page, int pageSize, String name) {
        //确保参数取到
        Page<Dish> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(name), Dish::getName, name);
        Page<Dish> page1 = dishService.page(pageInfo, wrapper);
        //多表
        Page<DishDto> dishDtoPage = new Page<>();
        //对象拷贝，page1.records
        BeanUtils.copyProperties(page1, dishDtoPage, "records");
        //获取分页查询的记录
        List<Dish> records = page1.getRecords();
        //dishDtoList 用来存储DishDto
        List<DishDto> dishDtoList = new ArrayList<>();
        //根据前面dish表的数据进行遍历，并且根据里面的category_id 进行查询category表查询，找到分类名称，然后赋值给dishDto
        for (Dish item : records) {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item, dishDto);
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            //如果能查询到数据
            if (category != null) {
                //设置名字赋值给dishDto中的categoryName这个字段
                dishDto.setCategoryName(category.getName());
            }
            //本次循环的数据添加到dishDtoList中
            dishDtoList.add(dishDto);
        }
        //将dishDtoList 赋值给dishDtoPage
        dishDtoPage.setRecords(dishDtoList);
        return R.success(dishDtoPage);
    }

    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto) {
        dishService.savedishWithFlavors(dishDto);
        return R.success("新增菜品成功");
    }


    @GetMapping("/{id}")
    public R<DishDto> get(@PathVariable Long id) {
        DishDto dishDto = dishService.getWithFlavor(id);
        return R.success(dishDto);
    }

    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto) {
        dishService.updateWithFlavor(dishDto);
        return R.success("修改成功");
    }

    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable Integer status, @RequestParam List<Long> ids) {
//    List<Dish> list=ids.stream().map(item -> {
//        Dish dish=new Dish();
//        dish.setId(item);
//        dish.setStatus(status);
//        return dish;
//    }).collect(Collectors.toList());
//        dishService.updateBatchById(list);

//    List<Dish> list=new ArrayList<>();
//    for (Long id : ids) {
//        Dish dish=new Dish();
//        dish.setId(id);
//        dish.setStatus(status);
//        list.add(dish);
//    }
//    dishService.updateBatchById(list);
        ids.forEach(item->{
            LambdaUpdateWrapper<Dish> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(Dish::getId, item).set(Dish::getStatus, status);
            dishService.update(updateWrapper);
        });
        //数组遍历
//    ids.forEach(System.out::println);
        //ids.forEach(item -> System.out.println(item));
        return R.success("修改成功");
    }
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        dishService.removeWithFlavor(ids);
        return R.success("删除成功");
    }
    @GetMapping("/list")
    public R<List<DishDto>> list(Dish dish){
        LambdaQueryWrapper<Dish> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getCategoryId,dish.getCategoryId());
        wrapper.eq(Dish::getStatus,1);
        wrapper.orderByDesc(Dish::getUpdateTime);
        List<Dish> list=dishService.list(wrapper);
        //准备口味相关数据
//        List<DishDto> dishDtoList=list.stream().map(item -> {
//            DishDto dishDto=new DishDto();
//            BeanUtils.copyProperties(item, dishDto);
//            Long dishId=item.getId();
//            LambdaQueryWrapper<DishFlavor> wrapper1=new LambdaQueryWrapper<>();
//            wrapper1.eq(DishFlavor::getDishId,dishId);
//            List<DishFlavor> list1=dishFlavorService.list(wrapper1);
//            dishDto.setFlavors(list1);
//            return dishDto;
//        }).collect(Collectors.toList());

        List<DishDto> dishDtoList=new ArrayList<>();
        for (Dish item : list) {
            DishDto dishDto=new DishDto();
            BeanUtils.copyProperties(item, dishDto);
            LambdaQueryWrapper<DishFlavor> wrapper1=new LambdaQueryWrapper<>();
            wrapper1.eq(DishFlavor::getDishId,item.getId());
            List<DishFlavor> list1=dishFlavorService.list(wrapper1);
            dishDto.setFlavors(list1);
            //准备categoryName
            Long categoryId=item.getCategoryId();
            Category category=categoryService.getById(categoryId);
            dishDto.setCategoryName(category.getName());
            dishDtoList.add(dishDto);
        }
        return R.success(dishDtoList);
    }
}
